package mspbots.timedoctor.client.core.api.v1;

import lombok.extern.log4j.Log4j2;
import mspbots.timedoctor.client.core.api.BaseApiService;
import mspbots.timedoctor.client.core.api.ResultMessageModel;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * data.connector.timedoctor.api.CompaniesService
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2020/3/25
 */
@Log4j2
@Service
public class CompaniesService extends BaseApiService {

    public void asyncCompanies(Long tenant) {
        this.doctorV1Client.get("/v1.1/companies", Map.of("tenantId", tenant))
                .subscribe(result -> this.messagingTemplate.convertAndSend("data.timedoctor.companies",
                        "timedoctor-companies-key",
                        ResultMessageModel.builder().action(ResultMessageModel.MessageModelAction.ADD)
                                .type(ResultMessageModel.MessageModelType.COMPANIES)
                                .sendTime(LocalDateTime.now()).entity(result).tenantId(tenant).build())
                        , err -> log.error("Companies sync data error, msg:{}", err.getMessage()));
    }


}
